<template>
  <div class="invoice-information">
    <div class="common-title">
      <h1>发票抬头</h1>
      <p>INVOICE TITLE</p>
    </div>
    <div class="invoice-information-radio">
      <van-radio-group v-model="invType" direction="horizontal">
        <van-radio name="1" checked-color="#15c182">企业</van-radio>
        <van-radio name="2" checked-color="#15c182">个人</van-radio>
      </van-radio-group>
    </div>
    <van-cell-group class="invoice-information-list">
      <van-field v-model="invTypeName" label="发票类型" input-align="right" :readonly="true" />
      <van-field v-model="invStyle" label="开票方式" input-align="right" :readonly="true" />
      <van-field v-model="invContent" label="发票内容" input-align="right" :readonly="true" />
    </van-cell-group>
    <van-cell-group v-if="invType==1">
      <van-field v-model="invCompany" class="required" label="单位名称" placeholder="请填写单位名称" />
      <van-field v-model="invCode" class="required" label="纳税人编号" placeholder="纳税人识别号／统一社会信用代码" />
      <van-field v-model="invRegAddr" label="企业地址" placeholder="公司注册地址" />
      <van-field v-model="invRegPhone" label="电话号码" placeholder="请填写" />
      <van-field v-model="invRegBname" label="开户银行" placeholder="公司开户银行" />
      <van-field v-model="invRegBaccount" label="银行账号" placeholder="公司开户银行帐号" />
    </van-cell-group>
    <div class="invoice-information-btn clear">
      <div @click="postInvoice">确认开票</div>
      <div class="close" @click="closeInvoice">关闭</div>
    </div>
  </div>
</template>
<script>
import { invoice } from "@/api/common";
export default {
  name: "index",
  data() {
    return {
      invType: "1",
      invTypeName: "增值税发票",
      invStyle: "纸质发票",
      invContent: "土壤15项检测",
      invState: "1",
      invTitle: "",
      invCompany: "",
      invCode: "",
      invRegAddr: "",
      invRegPhone: "",
      invRegBname: "",
      invRegBaccount: "",
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let invoiceInfo = JSON.parse(localStorage.getItem("invoiceInfo"));
      if (invoiceInfo) {
        this.invType = invoiceInfo.inv_type;
        this.invContent = invoiceInfo.inv_content;
        this.invState = invoiceInfo.inv_state;
        this.invTitle = invoiceInfo.inv_title;
        this.invCompany = invoiceInfo.inv_company;
        this.invCode = invoiceInfo.inv_code;
        this.invRegAddr = invoiceInfo.inv_reg_addr;
        this.invRegPhone = invoiceInfo.inv_reg_phone;
        this.invRegBname = invoiceInfo.inv_reg_bname;
        this.invRegBaccount = invoiceInfo.inv_reg_baccount;
      }
    },
    postInvoice() {
      if (this.invType == 1) {
        if (!this.invCompany) {
          this.$toast("请填写单位名称");
          return;
        } else if (!this.invCode) {
          this.$toast("请填写纳税人编号");
          return;
        }
      }
      let data = {
        inv_type: this.invType,
        inv_content: this.invContent,
        inv_state: this.invState,
        inv_title: this.invTitle,
        inv_company: this.invCompany,
        inv_code: this.invCode,
        inv_reg_addr: this.invRegAddr,
        inv_reg_phone: this.invRegPhone,
        inv_reg_bname: this.invRegBname,
        inv_reg_baccount: this.invRegBaccount,
        key: localStorage.getItem("userKey"),
      };
      invoice(data)
        .then((res) => {
          let code = res.data.code;
          if (code == 200) {
            this.$toast("保存成功");
            localStorage.setItem("invoiceInfo", JSON.stringify(data));
            localStorage.setItem("invoiceId", res.data.datas.invoice_id);
            setTimeout(() => {
              this.closeInvoice();
            }, 500);
          } else {
            this.$toast(res.data.datas.error);
          }
        })
        .catch((err) => {});
    },
    closeInvoice() {
      this.$router.push({
        name: "purchase-information",
      });
    },
  },
  filters: {},
  destroyed() {
    this.$toast.clear();
  },
};
</script>
<style scoped>
.invoice-information {
  width: 100%;
  position: relative;
  padding-top: 10px;
}

.invoice-information-inv-state {
  width: 100%;
  height: 47px;
  padding-left: 15px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
}
.invoice-information-inv-state .van-radio-group {
  display: flex;
  align-items: center;
}
.invoice-information-inv-state .van-radio {
  margin-right: 15px;
}
.invoice-information-inv-state > span {
  margin-right: 25px;
}

.invoice-information-radio {
  position: absolute;
  top: 20px;
  right: 10px;
  z-index: 99;
  width: 50%;
  height: 40px;
}
.invoice-information-radio .van-radio-group {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
}
.invoice-information-btn {
  width: 100%;
  background: #fff;
  padding: 35px 15px 50px;
  box-sizing: border-box;
}
.invoice-information-btn div {
  width: 40%;
  height: 40px;
  text-align: center;
  line-height: 40px;
  background: #39a794;
  margin: 0 5%;
  color: #fff;
  border-radius: 30px;
  border: 0;
  float: left;
  border: 1px solid #39a794;
  box-sizing: border-box;
}
.invoice-information-btn div.close {
  background: #fff;
  color: #39a794;
}
</style>